﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;
using BusinessLayer;

public partial class Administrator_AdminEdit : System.Web.UI.Page
{
    string cmd = "";
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"].ToString();
        switch (cmd)
        {
            case "new":
                rbStatus.Enabled = false;
                txtPass.ReadOnly = false;
                txtTen.Enabled = true;
                txtEmail.Enabled = true;
                txtTel.Enabled = true;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                break;
            case "edit":
                txtPass.ReadOnly = true;
                txtTen.Enabled = true;
                txtEmail.Enabled = true;
                rbStatus.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                if (!IsPostBack)
                {
                    FillData(Admin.GetOne(Id));
                }
                break;
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Admin admin = new Admin();
        admin.Admin_Name = txtTen.Text;
        admin.Admin_Email = txtEmail.Text;
        admin.Admin_Tel = txtTel.Text;
        admin.Password = txtPass.Text;
        Admin.Insert(admin);
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Admin admin = new Admin();
        admin.Admin_ID = Id;
        admin.Admin_Name = txtTen.Text;
        admin.Admin_Email = txtEmail.Text;
        admin.Admin_Tel = txtTel.Text;
        admin.Password = txtPass.Text;
        Admin.Update(admin);
    }
    protected void BbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin.aspx");
    }
    protected void FillData(Admin admin)
    {
        txtTen.Text = admin.Admin_Name;
        txtEmail.Text = admin.Admin_Email;
        txtPass.Text = admin.Password;
        txtTel.Text = admin.Admin_Tel;
        rbStatus.Items.FindByValue(admin.Admin_Status.ToString());
    }
}